/**
 * @file referee.C
 * @author kidneygood (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2022-11-18
 *
 * @copyright Copyright (c) 2022
 *
 */
#include "referee_task.h"
#include "robot_def.h"
#include "rm_referee.h"
#include "referee_UI.h"
#include "string.h"
#include "message_center.h"

static Referee_Interactive_info_t *Interactive_data; // UI绘制需要的机器人状态数据
static referee_info_t *referee_recv_info;            // 接收到的裁判系统数据
uint8_t UI_Seq;                                      // 包序号，供整个referee文件使用



/**
 * @brief  判断各种ID，选择客户端ID
 * @param  referee_info_t *referee_recv_info
 * @retval none
 * @attention
 */
static void DeterminRobotID()
{
    // id小于7是红色,大于7是蓝色,0为红色，1为蓝色   #define Robot_Red 0    #define Robot_Blue 1
    referee_recv_info->referee_id.Robot_Color = referee_recv_info->GameRobotState.robot_id > 7 ? Robot_Blue : Robot_Red;
    referee_recv_info->referee_id.Robot_ID = referee_recv_info->GameRobotState.robot_id;
    referee_recv_info->referee_id.Cilent_ID = 0x0100 + referee_recv_info->referee_id.Robot_ID; // 计算客户端ID
    referee_recv_info->referee_id.Receiver_Robot_ID = 0;
}

static void My_UI_Refresh(referee_info_t *referee_recv_info, Referee_Interactive_info_t *_Interactive_data);
static void Mode_Change_Check(Referee_Interactive_info_t *_Interactive_data); // 模式切换检测

// syhtod 正式上车后需删除
static void robot_mode_change(Referee_Interactive_info_t *_Interactive_data); // 测试用函数，实现模式自动变化

referee_info_t *Referee_Interactive_init(UART_HandleTypeDef *referee_usart_handle, Referee_Interactive_info_t *UI_data)
{
    referee_recv_info = RefereeInit(referee_usart_handle); // 初始化裁判系统的串口,并返回裁判系统反馈数据指针
    Interactive_data = UI_data;                            // 获取UI绘制需要的机器人状态数据

    return referee_recv_info;
}

void Referee_Interactive_task()
{

    My_UI_Refresh(referee_recv_info, Interactive_data);
}


static Graph_Data_t UI_Forward[2];      // 电容能量条
static Graph_Data_t UI_Flip[6];      // 电容能量条
static String_Data_t UI_Flip_state[4];  // 机器人状态,动态先add才能change
static Graph_Data_t UI_Lift[3];      // 电容能量条
//static Graph_Data_t UI_Roll[1];      // 电容能量条
static Graph_Data_t UI_ali[2];      // 电容能量条
static Graph_Data_t UI_save[4];      // 电容能量条
static Graph_Data_t UI_grab_ptich[2];      // 电容能量条
static String_Data_t UI_State_sta[4];  // 机器人状态,静态只需画一次
static String_Data_t UI_State_dyn[2];  // 机器人状态,动态先add才能change
#define LIFT_MIN 280
#define LIFT_TOTAL 352


void My_UI_init()
{
           while (referee_recv_info->GameRobotState.robot_id == 0)
        ;
    DeterminRobotID();
    UIDelete(&referee_recv_info->referee_id, UI_Data_Del_ALL, 0); // 清空UI


    // 底盘功率显示，静态
    Char_Draw(&UI_State_sta[0], "ss0_forward_state", UI_Graph_ADD, 7, UI_Color_Yellow, 18, 2, 620, 230, "Forward:");
    Char_ReFresh(&referee_recv_info->referee_id, UI_State_sta[0]);
    Char_Draw(&UI_State_sta[1], "ss1_lift_state", UI_Graph_ADD, 7, UI_Color_Yellow, 18, 2, 1670, 800, "lift:");
    Char_ReFresh(&referee_recv_info->referee_id, UI_State_sta[1]);
    // 能量条框
    Line_Draw(&UI_save[2], "ss2_save_line_left", UI_Graph_ADD, 0, UI_Color_Orange,2, 610, 150, 610 ,0);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_save[2]);
    Line_Draw(&UI_save[3], "ss3_save_line_right", UI_Graph_ADD, 1, UI_Color_Orange,2, 1310, 150, 1310 ,0);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_save[3]);

    Rectangle_Draw(&UI_Forward[0], "ss2_forward_rec", UI_Graph_ADD, 7, UI_Color_Green, 2, 720, 140, 1220, 180);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Forward[0]);
    Rectangle_Draw(&UI_Lift[0], "ss3_lift_rec", UI_Graph_ADD, 7, UI_Color_Green, 2, 1700, 800, 1740 ,350);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Lift[0]);

    Line_Draw(&UI_Forward[1], "sd0_forward_qua", UI_Graph_ADD, 8, UI_Color_Pink, 30, 720, 160, 1020, 160);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Forward[1]);
    Line_Draw(&UI_Lift[1], "sd1_lift_qua", UI_Graph_ADD, 8, UI_Color_Pink, 30, 1720, 450, 1720,350);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Lift[1]);
    Line_Draw(&UI_save[0], "sd2_save_long", UI_Graph_ADD, 8, UI_Color_Yellow, 4,150,700,150,800);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_save[0]);
    Line_Draw(&UI_save[1], "sd3_save_short", UI_Graph_ADD, 8, UI_Color_Yellow, 4,150,800,170,800);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_save[1]);
    Circle_Draw(&UI_grab_ptich[0], "ss4_grab_pitch_cir", UI_Graph_ADD, 8, UI_Color_Orange, 2,960,540,80);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_grab_ptich[0]);
    Line_Draw(&UI_grab_ptich[1], "sd4_grab_pitch_angle", UI_Graph_ADD, 8, UI_Color_Orange, 2,960,540,1040,540);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_grab_ptich[1]);
//空接标识线
    Line_Draw(&UI_ali[0], "ss5_ali_line_left", UI_Graph_ADD, 7, UI_Color_Purplish_red, 2, 860, 1080, 860 ,780);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_ali[0]);
    Line_Draw(&UI_ali[1], "ss9_ali_line_right", UI_Graph_ADD, 9, UI_Color_Purplish_red, 2, 1060, 1080, 1060 ,780);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_ali[1]);


    Rectangle_Draw(&UI_Flip[0], "ss6_flip_turn_rec", UI_Graph_ADD, 8, UI_Color_Green, 3,60,600,120,540);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Flip[0]);
    Rectangle_Draw(&UI_Flip[1], "ss7_flip_up_rec", UI_Graph_ADD, 8, UI_Color_Green, 3,130,600,190,540);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Flip[1]);
    Rectangle_Draw(&UI_Flip[2], "ss8_flip_down_rec", UI_Graph_ADD, 9, UI_Color_Green, 3,200,600,260,540);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Flip[2]);
    Line_Draw(&UI_Flip[3], "sd5_flip_state", UI_Graph_ADD, 8, UI_Color_Pink,58,90,599,90,541);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Flip[3]);
    // Line_Draw(&UI_Flip[3], "sd6", UI_Graph_ADD, 8, UI_Color_Pink,58,160,599,160,541);
    // UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Flip[3]);
    // Line_Draw(&UI_Flip[3], "sd6", UI_Graph_ADD, 8, UI_Color_Pink,58,230,599,230,541);
    // UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Flip[3]);
    Char_Draw(&UI_Flip_state[0], "sd6_flip_on_tip", UI_Graph_ADD, 8, UI_Color_Main,17,2,60,630,"Push ctrl to start");
    Char_ReFresh(&referee_recv_info->referee_id,  UI_Flip_state[0]);
    Char_Draw(&UI_Flip_state[1], "ss7_flip_state_turn", UI_Graph_ADD, 1, UI_Color_Green,12,2,60,530,"Turn");
    Char_ReFresh(&referee_recv_info->referee_id,  UI_Flip_state[1]);
    Char_Draw(&UI_Flip_state[2], "ss8_flip_state_up", UI_Graph_ADD, 8, UI_Color_Green,12,2,140,530,"up");
    Char_ReFresh(&referee_recv_info->referee_id,  UI_Flip_state[2]);
    Char_Draw(&UI_Flip_state[3], "ss9_flip_down", UI_Graph_ADD, 7, UI_Color_Green,12,2,210,530,"down");
    Char_ReFresh(&referee_recv_info->referee_id,  UI_Flip_state[3]);

    Line_Draw(&UI_Lift[2], "sd8_lift_pitch_height", UI_Graph_ADD, 9, UI_Color_Orange, 30, 1720, 480, 1720,450);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Lift[2]);

    Char_Draw(&UI_State_dyn[0], "sd6_speed_state", UI_Graph_ADD, 1, UI_Color_Main,30,2,1350,350,"Fast");
    Char_ReFresh(&referee_recv_info->referee_id,  UI_State_dyn[0]);

}
 uint32_t lift_qucoordinate_y;
static void My_UI_Refresh(referee_info_t *referee_recv_info, Referee_Interactive_info_t *_Interactive_data)
{
   
    
    Mode_Change_Check(_Interactive_data);

    
    if(_Interactive_data->Referee_Interactive_Flag.flip_flag == 1)
    {

    }
    if(_Interactive_data->Referee_Interactive_Flag.flip_ctrl_flag == 1)
    {
        
    }
    if(_Interactive_data->Referee_Interactive_Flag.lift_flag == 1)
    {
    lift_qucoordinate_y = 350 + 400*((_Interactive_data->lift - LIFT_MIN)/LIFT_TOTAL);
    if(lift_qucoordinate_y >= 750)
    {
        lift_qucoordinate_y = 750;
    }
    if(lift_qucoordinate_y <= 350)
    {
        lift_qucoordinate_y = 350;
    }
    Line_Draw(&UI_Lift[1], "sd1_lift_qua", UI_Graph_Change, 8, UI_Color_Pink, 30, 1720, lift_qucoordinate_y, 1720,350);
    UI_ReFresh(&referee_recv_info->referee_id, 1, UI_Lift[1]); 
    }
}

/**
 * @brief  模式切换检测,模式发生切换时，对flag置位
 * @param  Referee_Interactive_info_t *_Interactive_data
 * @retval none
 * @attention
 */
static void Mode_Change_Check(Referee_Interactive_info_t *_Interactive_data)
{
    
    if (_Interactive_data->flip_mode != _Interactive_data->flip_last_mode)
    {
        _Interactive_data->Referee_Interactive_Flag.flip_flag = 1;
        _Interactive_data->flip_last_mode = _Interactive_data->flip_mode;
    }

    if (_Interactive_data->forward != _Interactive_data->forward_last)
    {
        _Interactive_data->Referee_Interactive_Flag.forward_flag = 1;
        _Interactive_data->forward_last = _Interactive_data->forward;
    }

    if (_Interactive_data->lift >= _Interactive_data->lift_last +4 || _Interactive_data->lift <= _Interactive_data->lift_last - 4)
    {
        _Interactive_data->Referee_Interactive_Flag.lift_flag = 1;
        _Interactive_data->lift_last = _Interactive_data->lift;
    }
    else
    {
        _Interactive_data->Referee_Interactive_Flag.lift_flag = 0;
        _Interactive_data->lift_last = _Interactive_data->lift;
    }

    if (_Interactive_data->pitch_ang != _Interactive_data->pitch_ang_last)
    {
        _Interactive_data->Referee_Interactive_Flag.pitch_flag = 1;
        _Interactive_data->pitch_ang_last = _Interactive_data->pitch_ang;
    }

    if (_Interactive_data->save_ang != _Interactive_data->save_ang_last)
    {
        _Interactive_data->Referee_Interactive_Flag.save_flag = 1;
        _Interactive_data->save_ang_last = _Interactive_data->save_ang;
    }

    if (_Interactive_data->flip_ctrl != _Interactive_data->flip_ctrl_last)
    {
        _Interactive_data->Referee_Interactive_Flag.flip_ctrl_flag = 1;
        _Interactive_data->flip_ctrl_last = _Interactive_data->flip_mode;
    }
}
